﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.SalesManagement.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.ServiceEntity.MyAccount;
using RiderShop.Mobile.Common.Component;

namespace RiderShop.Mobile.Controllers.MyAccount
{
    public partial class MyAccountController : ThemeController
    {
        #region 用户收货地址列表
        /// <summary>
        /// 用户收货地址列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddress()
        {
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddressList(WebClientContext.Current.UserId);
            @ViewBag.UserId = WebClientContext.Current.UserId;
            @ViewBag.Number = 1;
            //return View(shippingAddressList);
            //return ManualView("ShippingAddress", shippingAddressList);
            return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddress", WebClientContext.Current.CacheKeyDefaultMasterPage, shippingAddressList);
        }
        #endregion

        #region 查看用户的某个收货地址
        /// <summary>
        /// 查看用户的某个收货地址
        /// </summary>
        /// <param name="userId">当前用户Id</param>
        /// <param name="shippingId">当前收货地址Id</param>
        /// <param name="number">当前第几个收货地址</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddressEdit(int userId, int shippingId, int number)
        {
            @ViewBag.UserId = userId;
            @ViewBag.ShippingId = shippingId;
            @ViewBag.Number = number;
            List<UserShipingAddressExtend> shippingAddress = MyAccountModel.GetShippingAddress(userId, shippingId);
            List<RegionBasicInfo> regionList = new List<RegionBasicInfo>();
            regionList = MyAccountModel.GetRegionListByParentId(0);
            shippingAddress[0].RegionList = regionList;

            if (shippingAddress.Count > 0)
            {
                //return View(shippingAddress);
                //return ManualView("ShippingAddressEdit", shippingAddress);
                return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddressEdit", WebClientContext.Current.CacheKeyDefaultMasterPage, shippingAddress);
            }
            //return View();
            //return ManualView("ShippingAddressEdit", null);
            return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddressEdit", WebClientContext.Current.CacheKeyDefaultMasterPage, null);
        }
        #endregion

        #region 保存用户编辑后的某个收货地址
        /// <summary>
        /// 保存用户编辑后的某个收货地址
        /// </summary>
        /// <param name="collection">表单集合对象</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult ShippingAddressEdit(FormCollection collection)
        {
            UserShipingAddressExtend shipingAddress = new UserShipingAddressExtend();
            shipingAddress.UserId = WebClientContext.Current.UserId;
            shipingAddress.ShippingId = Convert.ToInt32(collection["hidShippingId"]);
            shipingAddress.ShipTo = collection["txtUserName"];
            shipingAddress.CellPhone = collection["txtCellPhone"];
            shipingAddress.EmailAddress = string.IsNullOrEmpty(collection["txtEmailAddress"]) ? string.Empty : collection["txtEmailAddress"];
            shipingAddress.RegionId = Convert.ToInt32(collection["RegionId"]);
            shipingAddress.Zipcode = collection["txtPostcode"];
            shipingAddress.IsDefault = Convert.ToBoolean(collection["selIsDefault"]);
            shipingAddress.Address = collection["txtAddress"];

            if (string.IsNullOrEmpty(collection["selIsDefault"]) && !string.IsNullOrEmpty(Request.QueryString["action"]))
            {
                shipingAddress.IsDefault = true;
            }

            ServiceResponser result = MyAccountModel.EditShippingAddress(shipingAddress);
            if (result.Status)
            {

                var action = Request.QueryString["action"];
                if (action == "cart")
                {
                    return Redirect("~/Purchase/ShoppingCart");
                }
                else if (action == "order")
                {
                    return Redirect("~/Purchase/CheckOut");
                }

                return Redirect("~/MyAccount/ShippingAddress");
            }

            return Redirect("~/MyAccount/ShippingAddress");
        }
        #endregion

        #region 删除用户的某个收货地址
        /// <summary>
        /// 删除用户的某个收货地址
        /// </summary>
        /// <param name="collection">表单集合对象</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult ShippingAddressDelete(FormCollection collection)
        {
            UserShipingAddressExtend shipingAddress = new UserShipingAddressExtend();
            shipingAddress.UserId = Convert.ToInt32(collection["hidUserId"]);
            shipingAddress.ShippingId = Convert.ToInt32(collection["hidShippingId"]);

            ServiceResponser result = MyAccountModel.DeleteShippingAddress(shipingAddress.ShippingId);

            return Redirect("ShippingAddress?userId=" + shipingAddress.UserId + "&action=" + Request.QueryString["action"]);
        }
        #endregion

        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddressCreate()
        {
            UserShipingAddressExtend shipingAddress = new UserShipingAddressExtend();
            shipingAddress.UserId = WebClientContext.Current.UserId;
            List<RegionBasicInfo> regionList = new List<RegionBasicInfo>();
            regionList = MyAccountModel.GetRegionListByParentId(0);
            shipingAddress.RegionList = regionList;

            //return View(shipingAddress);
            //return ManualView("ShippingAddressCreate", shipingAddress);
            return View(WebClientContext.Current.SiteTheme, "MyAccount", "ShippingAddressCreate", WebClientContext.Current.CacheKeyDefaultMasterPage, shipingAddress);

        }

        [HttpPost]
        [Authorize]
        public ActionResult ShippingAddressCreate(UserShipingAddressExtend userShipingAddressExtend)
        {
            userShipingAddressExtend.UserId = WebClientContext.Current.UserId;
            var action = Request.QueryString["action"];

            if (!userShipingAddressExtend.IsDefault.HasValue && !string.IsNullOrEmpty(action))
            {
                userShipingAddressExtend.IsDefault = true;
            }

            ServiceResponser result = MyAccountModel.AddShippingAddress(userShipingAddressExtend);
            if (result.Status && !string.IsNullOrEmpty(action))
            {
                if (action.ToLower() == "order")
                {
                    return Redirect("~/Purchase/CheckOut");
                }
                else if (action == "cart")
                {
                    return Redirect("~/Purchase/ShoppingCart");
                }
            }

            return Redirect("~/MyAccount/ShippingAddress");
        }

        [HttpGet]
        [Authorize]
        public ActionResult ShippingAddressSelect()
        {
            int userId = WebClientContext.Current.UserId;
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddressList(userId);
            @ViewBag.Number = 1;
            if (shippingAddressList.Count > 0)
            {
                return View(shippingAddressList);
            }

            return View();
        }
        [HttpGet]
        [Authorize]
        public ActionResult SetDefaultShippingAddress(int shippingId)
        {
            int userId = WebClientContext.Current.UserId;
            List<UserShipingAddressExtend> shippingAddressList = MyAccountModel.GetShippingAddress(userId, shippingId);
            if (shippingAddressList.Count > 0)
            {
                shippingAddressList[0].IsDefault = true;
                ServiceResponser result = MyAccountModel.EditShippingAddress(shippingAddressList[0]);

            }

            var action = Request.QueryString["action"];
            if (!string.IsNullOrEmpty(action))
            {
                if (action.ToLower() == "order")
                {
                    return Redirect("~/Purchase/CheckOut");
                }
                else if (action == "cart")
                {
                    return Redirect("~/Purchase/ShoppingCart");
                }
            }

            return Redirect("~/MyAccount/ShippingAddress?" + "action=" + Request.QueryString["action"]);
        }

        /// <summary>
        /// 得到用户自提点列表
        /// </summary>
        /// <param name="lng">用户的经度</param>
        /// <param name="lat">用户的纬度</param>
        /// <returns>用户自提点列表</returns>
        [HttpGet]
        [Authorize]
        public ActionResult PickUpAddress(decimal lng = 0, decimal lat = 0)
        {
            int userId = WebClientContext.Current.UserId;
            var userPickAddressList = MyAccountModel.GetUserPickAddressList(userId, lng, lat);

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "PickUpAddress", WebClientContext.Current.CacheKeyDefaultMasterPage, userPickAddressList);
        }
    }
}
